﻿using Sustainalytics.ClientDW.Entities;
using Sustainalytics.DataLayer.Mongo;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sustainalytics.CompanySearch.StressTest
{
    /// <summary>
    /// Log stress test results.
    /// </summary>
    public class CompanySearchStressTestLogger
    {
        CompanySearchStressTestRepository Repo { get; set; }

        /// <summary>
        /// Ctor.
        /// </summary>
        public CompanySearchStressTestLogger()
        {
            // create repository for holding log entries
            this.Repo = CompanySearchStressTestRepository.GetCompanySearchRepository(ConfigurationManager.ConnectionStrings["ClientDW"].ConnectionString);
        }

        /// <summary>
        /// Clean all logs.
        /// </summary>
        public void Clean()
        {
            this.Repo.Clean();
        }

        /// <summary>
        /// Log result.
        /// </summary>
        /// <param name="testSpec"> Test spec used. </param>
        /// <param name="explain"> Explain gathered while testing. </param>
        public void Log(CompanySearchStressTestSpec testSpec, MongoExplain explain)
        {
            this.Repo.AddLogEntry(new CompanySearchStressTestLogEntry(
                testSpec: testSpec,
                explain: explain,
                exceptionMessage: null));
        }

        /// <summary>
        /// Log result.
        /// </summary>
        /// <param name="testSpec"> Test spec used. </param>
        /// <param name="exception"> Exception that occured. </param>
        public void Log(CompanySearchStressTestSpec testSpec, Exception exception)
        {
            this.Repo.AddLogEntry(new CompanySearchStressTestLogEntry(
                testSpec: testSpec,
                explain: null,
                exceptionMessage: exception.Message));
        }
    }
}
